package com.sqfw.project.learnRecord.controller;

import com.sqfw.common.utils.poi.ExcelUtil;
import com.sqfw.framework.aspectj.lang.annotation.Log;
import com.sqfw.framework.aspectj.lang.enums.BusinessType;
import com.sqfw.framework.web.controller.BaseController;
import com.sqfw.framework.web.domain.AjaxResult;
import com.sqfw.framework.web.page.TableDataInfo;
import com.sqfw.project.learnRecord.domain.LearnRecordInfo;
import com.sqfw.project.learnRecord.service.ILearnRecordInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.ByteArrayInputStream;
import java.util.List;

/**
 * 录音学习Controller
 *
 * @author zsj
 * @date 2024-09-23
 */
@RestController
@RequestMapping("/api/learnRecord")
public class LearnRecordInfoController extends BaseController {
    @Autowired
    private ILearnRecordInfoService learnRecordInfoService;

    /**
     * 查询录音学习列表
     */
    @PreAuthorize("@ss.hasPermi('api:learnRecord')")
    @GetMapping("/list")
    @Log(title = "查询录音学习列表")
    public TableDataInfo list(LearnRecordInfo learnRecordInfo) {
        startPage();
        List<LearnRecordInfo> list = learnRecordInfoService.selectLearnRecordInfoList(learnRecordInfo);
        return getDataTable(list);
    }

    /**
     * 导出录音学习列表
     */
    @PreAuthorize("@ss.hasPermi('api:learnRecord:export')")
    @Log(title = "录音学习", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LearnRecordInfo learnRecordInfo) {
        List<LearnRecordInfo> list = learnRecordInfoService.selectLearnRecordInfoList(learnRecordInfo);
        ExcelUtil<LearnRecordInfo> util = new ExcelUtil<LearnRecordInfo>(LearnRecordInfo.class);
        util.exportExcel(response, list, "录音学习数据");
    }

    /**
     * 获取录音学习详细信息
     */
    @PreAuthorize("@ss.hasPermi('api:learnRecord:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(learnRecordInfoService.selectLearnRecordInfoById(id));
    }

    /**
     * 新增录音学习
     */
    @PreAuthorize("@ss.hasPermi('api:learnRecord:add')")
    @Log(title = "录音学习", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody LearnRecordInfo learnRecordInfo) {
        return toAjax(learnRecordInfoService.insertLearnRecordInfo(learnRecordInfo));
    }

    /**
     * 修改录音学习
     */
    @PreAuthorize("@ss.hasPermi('api:learnRecord:edit')")
    @Log(title = "录音学习", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LearnRecordInfo learnRecordInfo) {
        return toAjax(learnRecordInfoService.updateLearnRecordInfo(learnRecordInfo));
    }

    /**
     * 删除录音学习
     */
    @PreAuthorize("@ss.hasPermi('api:learnRecord:remove')")
    @Log(title = "录音学习", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(learnRecordInfoService.deleteLearnRecordInfoByIds(ids));
    }

    /**
     * 调听录音
     */
    @PreAuthorize("@ss.hasPermi('api:learnRecord')")
    @GetMapping("/play")
    @Log(title = "调听录音")
    public ResponseEntity<InputStreamResource> playRecord(String fileName) throws Exception {
        MediaType contentType;
        if (fileName.endsWith(".mp3")) {
            contentType = MediaType.valueOf("audio/mpeg");
        } else if (fileName.endsWith(".wav")) {
            contentType = MediaType.valueOf("audio/wav");
        } else {
            return ResponseEntity.badRequest().build(); // 不支持的文件类型
        }

        byte[] fileData = learnRecordInfoService.playRecord(fileName);
        InputStreamResource resource = new InputStreamResource(new ByteArrayInputStream(fileData));
        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=" + fileName.substring(fileName.lastIndexOf("/") + 1))
                .contentType(contentType)
                .body(resource);
    }
}
